package com.api;

import com.bean.ResponseBean;
import com.pojo.Room;
import com.service.RoomService;
import com.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@RestController
@RequestMapping("/api/v1")
public class ApkAction {

    @Autowired
    private RoomService roomService;

    private RedisTemplate redisTemplate;

    @Autowired
    private ApplicationContext applicationContext;
    @Value("${dy.home}")
    private String home;


    @Value("${server.port}")
    private String port;

    @RequestMapping("/apk/upgrade")
    public ResponseBean  upgrade(String mac, String versionCode)
    {
        Map map = new HashMap();
        map.put("version", 1);
        map.put("url", "");
        Room room = roomService.getRoomByMac(mac);
        if (room==null)
        {
            return new ResponseBean(404,"终端不存在",null);
        }
        if (redisTemplate==null)
        {
            redisTemplate = (RedisTemplate)applicationContext.getBean("StringRedisTemplate");
        }
        String  upgrade_md5 = (String)redisTemplate.boundValueOps("upgrade_md5").get();
        String  upgrade_filename = (String)redisTemplate.boundValueOps("upgrade_filename").get();
        String  upgrade_msg = (String)redisTemplate.boundValueOps("upgrade_msg").get();
        String  upgrade_version = (String)redisTemplate.boundValueOps("upgrade_version").get();
        String  upgrade_forceUpgrade = (String)redisTemplate.boundValueOps("upgrade_forceUpgrade").get();
        if (upgrade_version != null && versionCode!=null) {
            if (Long.parseLong(upgrade_version)>Long.parseLong(versionCode))
            {
                File apkFile = new File(home+"apk",upgrade_filename);
                if (apkFile.exists()==false)
                {
                    return new ResponseBean(404,"apk文件不存在",null);
                }
                String ip  = null;
                InputStream is = null;
                try {
                    is = new FileInputStream("ip.properties");
                    Properties p = new Properties();
                    p.load(is);
                    String ip2 = p.getProperty("server.ip");
                    if (StringUtils.isEmpty(ip2)==false)
                    {
                        ip = "http://"+ip2+":"+port+"/";
                    }

                } catch (Exception e) {
                    return new ResponseBean(500,"IP未配置",null);
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                        // e.printStackTrace();
                    }
                }
                if (ip!=null && ip.endsWith("/"))
                {
                    ip = ip.substring(0,ip.length()-1);
                }

                map.put("version", upgrade_version); //新版本
                map.put("url", ip +"/apk/"+ upgrade_filename);
                map.put("msg",upgrade_msg); //升级时提示信息
                map.put("forceUpgrade",upgrade_forceUpgrade); //是否强制升级
                return new ResponseBean(200,"success", map);
            }

        }

        return new ResponseBean(500, "不要升级", map);
    }
}
